﻿using CefSharp.WinForms;
using CefSharp.WinForms.Vue3.Tool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace CefSharp.WinForms.Vue3
{
    public partial class Form1 : Form
    {
        public ChromiumWebBrowser webCom;
        public BrowerFun bound;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string baseUrl = "http://192.168.30.166:7777/";
            string loadIndexUrl = (baseUrl + @"index.html");//加载起始页
            CefSettings settings = new CefSettings();
            settings.CefCommandLineArgs.Add("disable-gpu", "1"); // 禁用 GPU 加速
            settings.MultiThreadedMessageLoop = true;
            settings.CefCommandLineArgs.Add("--disable-web-security", "");//允许跨域
            settings.CefCommandLineArgs.Add("enable-media-stream", "1");
            settings.CefCommandLineArgs.Add("allow-file-access-from-files", "1");
            settings.CefCommandLineArgs.Add("enable-logging", "1"); // 启用日志输出
            settings.CefCommandLineArgs.Add("log-file", AppDomain.CurrentDomain.BaseDirectory + "/log/log.txt"); // 指定日志文件路
            settings.LogSeverity = LogSeverity.Verbose; // 启用详细日志
            settings.LogFile = AppDomain.CurrentDomain.BaseDirectory+"/cefsharp.log"; // 日志文件路径

            Cef.Initialize(settings);
            //Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
            webCom = new CefSharp.WinForms.ChromiumWebBrowser(loadIndexUrl);//传入地址，实例化页面对象
            bound = new BrowerFun(webCom, this);

            //79.0以上的新的方法注册  注册JS调用对象  (该段代码需要写在浏览器加入控件之前)
            webCom.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
            webCom.JavascriptObjectRepository.Register("browser", bound, isAsync: false, options: BindingOptions.DefaultBinder);

            panelContainer.Dock = DockStyle.Fill;
            webCom.Dock = DockStyle.Fill;//指定页面停靠位置和方式
            panelContainer.Controls.Add(webCom);//向panel1控件里添加页面
            webCom.Load(loadIndexUrl);//加载页面
            //监听按键操作
            webCom.KeyboardHandler = new CEFKeyBoardHander();


        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                WebFun web = new WebFun(webCom);
                web.Message();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
